package cn.com.cfae.iras.doc.analyze.components.integrity;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import cn.com.cfae.iras.doc.analyze.parse.model.PageModel;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.*;

@Component("I1020")
@Scope("prototype")
public class I1020 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(I1020.class);

    @Autowired
    private HandingData handingData;
    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取I1020组件M");
        //"企业基本情况披露——安全生产"
        List<ComponentResult> componentResults = checkRulesI1020("M-5",getCatalogId(),"M-5-8",getRuleTitle(),"(M1)");
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取I1020组件BM");
        List<ComponentResult> componentResults = checkRulesI1020("BM-5","BM-5","BM-5-2",getRuleTitle(),"(M1)");
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        log.info("获取I1020组件DM");
        List<ComponentResult> componentResults = checkRulesI1020("DM-5","DM-5","DM-5-8",getRuleTitle(),"(D1)");
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        log.info("获取I1020组件BDM");
        List<ComponentResult> componentResults = checkRulesI1020("BDM-5","BDM-5","BDM-5-2",getRuleTitle(),"(D1)");
        return componentResults;
    }

    private List<ComponentResult> checkRulesI1020(String model,String catalogId,String chapterId,String ruleName,String subTitle){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        ExtractItem extractItem = null;
        int pageNum = 0;
        List<ComponentResult.TextPosition> contentAndPositionList = new ArrayList<>();
        ComponentResult.TextPosition contentAndPosition =null;
        boolean flag = false;

        int startPage = 0;
        Double x = 0.0;
        Double y = 0.0;
        boolean rdFlag = false;
        boolean zgFlag = false;
        boolean ysFlag = false;
        boolean isAllErr = false;
        try {
            if (extractData!=null){
                ChapterModel chapterModel = extractData.getChapterModel(model);
                startPage =  chapterModel!=null?chapterModel.getStartPage():0;
                if (chapterModel!=null){
                    List<PageModel> pageModelList = chapterModel.getPageModelList();
                    if (CollectionUtils.isNotEmpty(pageModelList)){
                        for (PageModel pageModel : pageModelList){
                            JsonElement jsonTextData = pageModel.getJsonTextData();
                            if (!jsonTextData.isJsonNull()){
                                JsonArray jsonArray = jsonTextData.getAsJsonArray();
                                if (!jsonArray.isJsonNull()){
                                    for (int i=0;i<jsonArray.size();i++){
                                        JsonObject asJsonObject = jsonArray.get(i).getAsJsonObject();
                                        Set set = asJsonObject.entrySet();
                                        Iterator<Map.Entry<String, JsonElement>> it = set.iterator();
                                        String key ;
                                        while (it.hasNext()){
                                            Map.Entry<String, JsonElement> entry = it.next();
                                            key= entry.getKey();
                                            if (StringUtils.isNotEmpty(key)&&key.equals("data")){
                                                JsonArray dataJsonArray = null;
                                                if (entry.getValue().isJsonArray()){
                                                    dataJsonArray  = entry.getValue().getAsJsonArray();
                                                }
                                                if (!dataJsonArray.isJsonNull()){

                                                    for (int d=0; d<dataJsonArray.size(); d++){
                                                        JsonArray delmJsonArray = dataJsonArray.get(d).getAsJsonArray();
                                                        if (!dataJsonArray.isJsonNull()){
                                                            for (int de=0;de<delmJsonArray.size();de++){
                                                                JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();
                                                                Set deSet = deJsonObject.entrySet();
                                                                Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                                                                while (deIt.hasNext()){
                                                                    Map.Entry<String, JsonElement> deEntry = deIt.next();
                                                                    String deKey= deEntry.getKey();
                                                                    if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                                                                        y = deEntry.getValue()!=null?deEntry.getValue().getAsDouble():0.0;
                                                                    }
                                                                    if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                                                                        x = deEntry.getValue()!=null?deEntry.getValue().getAsDouble():0.0;
                                                                    }
                                                                    if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                                                                        if (!flag&&deEntry.getValue().getAsString().contains("安全生产事故")){
                                                                            pageNum = pageModel.getPageNumber();
                                                                            pageNum = pageNum!=0?pageNum:startPage;
                                                                            contentAndPosition =  new ComponentResult().new TextPosition();
                                                                            contentAndPosition.setX(x);
                                                                            contentAndPosition.setY(y);
                                                                            contentAndPosition.setContent(deEntry.getValue().getAsString());
                                                                            contentAndPositionList.add(contentAndPosition);
                                                                            componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTitle+"安全生产事故","正确",false,getRegulationId(),pageNum,contentAndPositionList,"01");
                                                                            componentResults.add(componentResult);
                                                                            flag = true;
                                                                        }
                                                                        if (flag){
                                                                            if (!rdFlag&&deEntry.getValue().getAsString().contains("认定")){
                                                                                pageNum = pageModel.getPageNumber();
                                                                                pageNum = pageNum!=0?pageNum:startPage;
                                                                                contentAndPosition =  new ComponentResult().new TextPosition();
                                                                                contentAndPosition.setX(x);
                                                                                contentAndPosition.setY(y);
                                                                                contentAndPosition.setContent(deEntry.getValue().getAsString());
                                                                                contentAndPositionList.add(contentAndPosition);
                                                                                componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTitle+"认定","正确",false,getRegulationId(),pageNum,contentAndPositionList,"01");
                                                                                componentResults.add(componentResult);
                                                                                rdFlag = true;
                                                                            }
                                                                            else if (!zgFlag&&deEntry.getValue().getAsString().contains("整改")){
                                                                                pageNum = pageModel.getPageNumber();
                                                                                pageNum = pageNum!=0?pageNum:startPage;
                                                                                contentAndPosition =  new ComponentResult().new TextPosition();
                                                                                contentAndPosition.setX(x);
                                                                                contentAndPosition.setY(y);
                                                                                contentAndPosition.setContent(deEntry.getValue().getAsString());
                                                                                contentAndPositionList.add(contentAndPosition);
                                                                                componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTitle+"整改","正确",false,getRegulationId(),pageNum,contentAndPositionList,"01");
                                                                                componentResults.add(componentResult);
                                                                                zgFlag = true;
                                                                            }
                                                                            else if (!ysFlag&&deEntry.getValue().getAsString().contains("验收")){
                                                                                pageNum = pageModel.getPageNumber();
                                                                                pageNum = pageNum!=0?pageNum:startPage;
                                                                                contentAndPosition =  new ComponentResult().new TextPosition();
                                                                                contentAndPosition.setX(x);
                                                                                contentAndPosition.setY(y);
                                                                                contentAndPosition.setContent(deEntry.getValue().getAsString());
                                                                                contentAndPositionList.add(contentAndPosition);
                                                                                componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTitle+"验收","正确",false,getRegulationId(),pageNum,contentAndPositionList,"01");
                                                                                componentResults.add(componentResult);
                                                                                ysFlag = true;
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                                if (rdFlag &&zgFlag&&ysFlag){
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                        if (rdFlag &&zgFlag&&ysFlag){
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            if (rdFlag &&zgFlag&&ysFlag){
                                                break;
                                            }
                                        }
                                        if (rdFlag &&zgFlag&&ysFlag)
                                            break;
                                    }
                                }
                            }
                            if (rdFlag &&zgFlag&&ysFlag)
                                break;
                        }
                    }
                }
            }

            if (!flag){
                componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTitle+"安全生产事故","未检查到,建议人工核对",true,getRegulationId(),startPage,null,"03");
                componentResults.add(componentResult);
                componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"认定", "未触发检查", true, getRegulationId(), startPage, null, "03");
                componentResults.add(componentResult);
                componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"整改", "未触发检查", true, getRegulationId(), startPage, null, "03");
                componentResults.add(componentResult);
                componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTitle+"验收","未触发检查",true,getRegulationId(),startPage,null,"03");
                componentResults.add(componentResult);
            }else if (flag){
                if (!rdFlag) {
                    isAllErr = true;
                    componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"认定", "缺失", true, getRegulationId(), startPage, null, "02");
                    componentResults.add(componentResult);
                }
                if (!zgFlag){
                    isAllErr = true;
                    componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"整改", "缺失", true, getRegulationId(), startPage, null, "02");
                    componentResults.add(componentResult);
                }
                if (!ysFlag){
                    isAllErr = true;
                    componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTitle+"验收","缺失",true,getRegulationId(),startPage,null,"02");
                    componentResults.add(componentResult);
                }
            }
            if (CollectionUtils.isNotEmpty(componentResults)){
                if (isAllErr){
                    for (ComponentResult result:componentResults){
                        result.setExist("02");
                    }
                }
            }

        }catch (Exception e){
            componentResults.clear();
            componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTitle+"安全生产事故","未识别,建议人工核对",true,getRegulationId(),startPage,null,"03");
            componentResults.add(componentResult);
            componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"认定", "未识别,建议人工核对", true, getRegulationId(), startPage, null, "03");
            componentResults.add(componentResult);
            componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, subTitle+"整改", "未识别,建议人工核对", true, getRegulationId(), startPage, null, "03");
            componentResults.add(componentResult);
            componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTitle+"验收","未识别,建议人工核对",true,getRegulationId(),startPage,null,"03");
            componentResults.add(componentResult);
            log.error(e.getMessage(),e);
        }

        return componentResults;
    }
}
